<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="logo-container">
          <img src="/vite.svg" alt="Logo" class="logo" />
        </div>
        <h1>图书管理系统</h1>
        <p class="subtitle">后台管理登录</p>
      </div>
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <el-icon class="input-icon"><User /></el-icon>
          <input
            type="text"
            v-model="username"
            placeholder="请输入用户名"
            required
          />
        </div>

        <div class="form-group">
          <el-icon class="input-icon"><Lock /></el-icon>
          <input
            :type="showPassword ? 'text' : 'password'"
            v-model="password"
            placeholder="请输入密码"
            required
          />
          <el-icon 
            class="password-toggle" 
            @click="showPassword = !showPassword"
          >
            <component :is="showPassword ? 'View' : 'Hide'" />
          </el-icon>
        </div>

        <div class="form-options">
          <label class="remember-me">
            <el-checkbox v-model="rememberMe" size="large">记住我</el-checkbox>
          </label>
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>

        <el-button type="primary" native-type="submit" class="login-btn" :loading="loading">
          登 录
        </el-button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { login } from "../apis/sysUser";
import { setToken } from "../utils/cookie";
import { ElMessage } from "element-plus";
import router from "../router/router";

const username = ref("admin");
const password = ref("123456");
const rememberMe = ref(false);
const showPassword = ref(false);
const loading = ref(false);

const handleLogin = async () => {
  try {
    loading.value = true;
    const res = await login({
      username: username.value,
      password: password.value,
    });
    ElMessage.success("登录成功");
    setToken(res.data);
    router.push('/layout/dashboard');
  } catch (error) {
    console.error("登录失败:", error);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  padding: 1rem;
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  width: 300px;
  height: 300px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  top: -100px;
  right: -100px;
}

.login-container::after {
  content: '';
  position: absolute;
  width: 200px;
  height: 200px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  bottom: -50px;
  left: -50px;
}

.login-box {
  background: rgba(255, 255, 255, 0.98);
  padding: 2.5rem;
  border-radius: 1rem;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
  width: 100%;
  max-width: 450px;
  position: relative;
  z-index: 10;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.logo-container {
  background: rgba(24, 144, 255, 0.1);
  width: 80px;
  height: 80px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1rem;
  box-shadow: 0 5px 15px rgba(24, 144, 255, 0.2);
}

.logo {
  width: 45px;
  height: 45px;
}

h1 {
  color: #2d3748;
  font-size: 1.8rem;
  margin: 0;
  font-weight: 600;
}

.subtitle {
  color: #718096;
  margin-top: 0.5rem;
  font-size: 0.95rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  position: relative;
}

.form-group input {
  width: 100%;
  padding: 0.9rem 1rem 0.9rem 2.8rem;
  border: 1px solid #e2e8f0;
  border-radius: 0.5rem;
  font-size: 1rem;
  transition: all 0.3s ease;
  color: #2d3748;
  background-color: #f8fafc;
}

.form-group input:focus {
  outline: none;
  border-color: #1890ff;
  box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.1);
  background-color: #fff;
}

.form-group input::placeholder {
  color: #a0aec0;
}

.input-icon {
  position: absolute;
  left: 1rem;
  top: 50%;
  transform: translateY(-50%);
  color: #a0aec0;
  font-size: 1.1rem;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: -0.5rem;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #718096;
  cursor: pointer;
}

.forgot-password {
  color: #1890ff;
  text-decoration: none;
  font-size: 0.9rem;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #096dd9;
  text-decoration: underline;
}

.login-btn {
  width: 100%;
  padding: 0.9rem;
  font-size: 1.1rem;
  font-weight: 500;
  margin-top: 0.5rem;
  border-radius: 0.5rem;
  transition: all 0.3s ease;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(24, 144, 255, 0.4);
}

.login-btn:active {
  transform: translateY(0);
}

.password-toggle {
  position: absolute;
  right: 1rem;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  user-select: none;
  font-size: 1.1rem;
  color: #a0aec0;
  transition: color 0.2s ease;
}

.password-toggle:hover {
  color: #1890ff;
}

@media (max-width: 480px) {
  .login-box {
    padding: 2rem 1.5rem;
  }
  
  h1 {
    font-size: 1.5rem;
  }
  
  .logo-container {
    width: 70px;
    height: 70px;
  }
  
  .logo {
    width: 40px;
    height: 40px;
  }
}
</style>
